package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.project.system.domain.SysModuleFieldSet;
import com.dhecp.project.system.service.SysModuleFieldSetService;

/**
 * 功能列设置 控制层
 * @author lwl
 * @version 2019年11月18日
 */
@Controller
@RequestMapping("/system/modulefieldset")
public class SysModuleFieldSetController extends BaseController{

	private String prefix = "system/modulefieldset";
	private String moduleTable = ModuleTable.MODULE_FIELD_SET;
	
	@Autowired
	private SysModuleFieldSetService moduleFieldSetService;
	
	@RequiresPermissions("system:modulefieldset:view")
    @GetMapping()
	public ModelAndView dictData(@RequestParam Map<String, Object> params)
    {	
		ModelAndView mv = this.getModelAndView();
		mv.addObject("moduleTable", moduleTable);
		mv.setViewName(prefix + "/modulefieldset");
		return mv;
    }
	
	/**
     * 根据 表名查询功能列设置列表
     * @param params
     * @return TableDataInfo
     * @author lwl
     */
	@RequiresPermissions("system:modulefieldset:list")
    @RequestMapping("/tablelist")
    @ResponseBody
    public TableDataInfo tablelist(@RequestParam Map<String, Object> params)    {
        List<SysModuleFieldSet> dictList = new ArrayList<SysModuleFieldSet>();
		try {
			dictList = moduleFieldSetService.queryModuleFieldSetByTableName(params);
		} catch (Exception e) {
			e.printStackTrace();
			return getDataTable(this.getExceptionMessage(e).toString());
		}
        return getDataTable(dictList);
    }
	
	/**
     * 打开字段功能列设置修改页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("system:modulefieldset:edit")
    @GetMapping("/edit")
    public ModelAndView edit(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	ModelAndView mv = this.getModelAndView();
        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "guid","字段编号");
        }
        if (mv.isEmpty()){
            AjaxResult ajaxResult = new AjaxResult();
            try{
            	SysModuleFieldSet moduleFieldSet = moduleFieldSetService.queryModuleFieldSetByGuid(params.get("guid").toString());
                if (moduleFieldSet == null){
                    ajaxResult = AjaxResult.error("没有符合条件的数据。");
                }else{
                    ajaxResult = AjaxResult.success("查询成功。");
                }
                mv.addObject("moduleFieldSet", moduleFieldSet);
            }catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/modulefieldset_edit");
        }
        return mv;
    }
    
    /**
     * 修改功能列设置信息--提交数据
     * @param dictionaryForFieldItems
     * @return AjaxResult
     */
    @RequiresPermissions("system:modulefieldset:edit")
    @Log(title = "修改功能列设置", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("moduleFieldSetItems") == null || params.get("moduleFieldSetItems") == ""){
        	return AjaxResult.error("缺少参数moduleFieldSetItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= moduleFieldSetService.updateModuleFieldSet(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
}
